require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32/process'
require 'win32ole'

class TC_PostingPerformancetest < Test::Unit::TestCase
    
    def startup
      $FilePath1 ='../resources/Images/gallery_slideshow.gif'
      $Rank1 = 'rank1'
      $Rank1_Edit = 'rank1_Edit'
      $FilePath2 ='../resources/Images/gallery_slideshow.gif'
      @@ie = Watir::IE.start($SITE_URL)
    end

    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_0000
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id

  
    end  
    
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
        
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
      end
      
    def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
      end
      
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
      end

    def test_08_navigateToSiteAdministration
        ie.goto($SITE_URL + 'controlpanel/Settings/default.aspx')
        assert(ie.contains_text('Site Administration'))      
      end

    def test_09_verifyLinksExist
        # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Setup').exists?)
        assert(ie.link(:text, 'Manage Licenses').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      
    def test_10_navigateToManageMemberPointsSystem
        ie.goto($SITE_URL + 'controlpanel/settings/ManageRanks.aspx')
      end

    def test_11_navigateToRankSettings
        assert(ie.radio(:id, /PostingActivityDisplay_0$/).exists?)
        assert(ie.radio(:id, /PostingActivityDisplay_1$/).exists?)
        ie.radio(:id, /PostingActivityDisplay_0$/).set
        ie.radio(:id, /PostingActivityDisplay_1$/).clear
        assert(ie.radio(:id, /DisplayUserRankAsPicture_0$/).exists?)
        assert(ie.radio(:id, /DisplayUserRankAsPicture_1$/).exists?)
        ie.radio(:id, /DisplayUserRankAsPicture_0$/).set
        ie.radio(:id, /DisplayUserRankAsPicture_1$/).clear
        assert(ie.button(:id, /SaveButton$/).exists?)  
        ie.button(:id, /SaveButton$/).click
      end
      
    def test_12_navigateToManageRank
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl02.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl02.GetTabAtIndex(1))")
        assert(ie.button(:value ,'Add A New Rank').exists?)
        ie.button(:value ,'Add A New Rank').click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id, /RankName$/).exists?)
        ie.frame(:index,1).text_field(:id, /RankName$/).value = $Rank1
        assert(ie.frame(:index,1).text_field(:id ,/ctl00_bcr_RankMinPost$/).exists?)
        ie.frame(:index,1).text_field(:id, /ctl00_bcr_RankMinPost$/).value = '5'
        assert(ie.frame(:index,1).text_field(:id, /ctl00_bcr_RankMaxPost$/).exists?)
        ie.frame(:index,1).text_field(:id, /ctl00_bcr_RankMaxPost$/).value = '20'
        #ie.fileField(:name, /AvatarUpload$/).set($FilePath)
        #assert(ie.fileField(:name, /AvatarUpload$/).exists?)
        #~ assert(ie.frame(:index,1).fileField(:name, /RankImageUpload$/).exists?)
        #~ sleep 5
        #~ ie.frame(:index,1).fileField(:name, /RankImageUpload$/).set($FilePath1)
        sleep 2
        assert(ie.frame(:index,1).link(:id, /SaveButton$/).exists?)
        ie.frame(:index,1).link(:id,/SaveButton$/).click
      end

    def test_13_editRank
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl02.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl02.GetTabAtIndex(1))")
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id, 'Listing')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][2].link(:text,$Rank1).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Edit').click
               break
             end   
         rowcount1=rowcount1+1
       end
       sleep 5
       assert(ie.frame(:index,1).text_field(:id, /RankName$/).exists?)
       ie.frame(:index,1).text_field(:id, /RankName$/).value = $Rank1_Edit
       assert(ie.frame(:index,1).text_field(:id ,/ctl00_bcr_RankMinPost$/).exists?)
       ie.frame(:index,1).text_field(:id, /ctl00_bcr_RankMinPost$/).value = '5'
       assert(ie.frame(:index,1).text_field(:id, /ctl00_bcr_RankMaxPost$/).exists?)
       ie.frame(:index,1).text_field(:id, /ctl00_bcr_RankMaxPost$/).value = '20'
       assert(ie.frame(:index,1).link(:id, /SaveButton$/).exists?)
       ie.frame(:index,1).link(:id,/SaveButton$/).click
     end

    def test_14_deleteRank
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl02.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl02.GetTabAtIndex(1))")
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id, 'Listing')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][2].link(:text,$Rank1_Edit).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Edit').click
               break
             end   
         rowcount1=rowcount1+1
       end
       sleep 5
       assert(ie.frame(:index,1).link(:id,/DeleteButton$/).exists?)
       ie.frame(:index,1).link(:id,/DeleteButton$/).click
     end

    def test_99_closeIE
      ie.close
    end
    
end 
       